<?php
/**
 * Description of fwiForm
 *
 * @author trunghpb
 */
class fwiForm {
    private $aryParam = null;
    private $mapData = null;
    private $valid = true;
    private $message = null;

    public function mapDB($paramKey) {
        return new ParamMaper($paramKey);
    }

    public function setAryParam($aryParam) {
        $this->aryParam = $aryParam;
        foreach ($this->aryParam as $key => $value) {
            $this->mapData[$key] = $value->getValue;
            foreach($value->getVtor() as $key => $vtor) {
                if (!fwiVtor::$vtor($value->getValue)) {
                    $this->message[] = $value->getMsg($key);
                }
            }

        }
    }

    public function toData() {
        return $this->mapData;
    }

    public function toMsg() {
        return implode($this->message, "\r\n");
    }

    public function getMsg() {
        return $this->message;
    }

    public function isNotValid() {
        return !$this->valid;
    }

    public static function load($form, $method) {
        $tmpForm = _fwiload::loadLogic($form);
        $tmpForm->setAryParam($tmpForm->$method());
        return $tmpForm;
    }
}


class ParamMaper {
    private $vtor = array();
    private $message = array();
    private $paramKey = null;
    private $paramValue = null;

    public function getValue() {
        return $this->paramValue;
    }
    public function  __construct($paramKey) {
        $this->paramKey = $paramKey;
        $this->paramValue = _fwig::$req->getParam($paramKey);
    }

    public function add($validator, $message) {
        $this->vtor[] = $validator;
        $this->message[] = $message;
    }

    public function getVtor() {
        return $this->vtor;
    }

    public function getMsg($i) {
        return $this->message[$i];
    }

}

class fwiVtor {
    public static $_VAL_BLANK = 'isBlank';
    public static $_VAL_SETTED = 'isSeted';
    public static $_VAL_DATEVALID = 'isDateValid';
    public static $_VAL_EQUAL = 'isEqual';

    public static function isBlank($data) {
        return $data === '';
    }

    public static function isSeted($data) {
        return isset($data);
    }

    public static function isDateValid($data) {
        return true;
    }

    public static function isEqual($data, $matchData) {
        return $data === $matchData;
    }
}

?>
